/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/

#ifndef STL2STRING_H
	#define STL2STRING_H


#include <boost/tokenizer.hpp>
#include <pgl/markup/STLXMLParser.h>

namespace markup
{

template <typename T>
class TSTL2String
{
public:
	static void ToString(const std::vector<T>& v, CString& str);
	static void ToString(const std::valarray<T>& v, CString& str);
	static void ToString(const std::list<T>& v, CString& str);

	static void ToSTL(LPCTSTR str, std::vector<T>& v);
	static void ToSTL(LPCTSTR str, std::valarray<T>& v);
	static void ToSTL(LPCTSTR str, std::list<T>& v);

	static void ByteToSTL(LPCTSTR str, std::vector<BYTE>& v);
	static void ByteToSTL(LPCTSTR str, std::valarray<BYTE>& v);
	static void ByteToSTL(LPCTSTR str, std::list<BYTE>& v);
};

template<typename T>
void TSTL2String<T>::ByteToSTL(LPCTSTR str, std::vector<BYTE>& v)
{
	std::_tistringstream in(static_cast<const TCHAR*>(str));
	BYTE vi;
	short s;

	// reading data...
	v.clear();
	while ( !in.eof() && in.good())
	{
		in>>s;
		vi=static_cast<BYTE>(s);

		if (in.good())
			v.push_back(vi);
	}
};

template<typename T>
void TSTL2String<T>::ByteToSTL(LPCTSTR str, std::valarray<BYTE>& v)
{

	std::_tistringstream in(static_cast<const TCHAR*>(str));
	std::vector<BYTE> lVi;
	BYTE vi;
	short s;

	// reading data...
	while ( !in.eof() && in.good())
	{
		in>>s;
		vi=static_cast<BYTE>(s);

		if (in.good())
			lVi.push_back(vi);
	}

	// ouputting to vector
	v.resize(lVi.size());
	for (UINT i =0;i<v.size();i++)
	{
		v[i]=lVi[i];
	}
};

template<typename T>
void TSTL2String<T>::ByteToSTL(LPCTSTR str, std::list<BYTE>& _l)
{
	std::_tistringstream in(static_cast<const TCHAR*>(str));
	BYTE vi;
	short s;

	_l.clear();
	// reading data...
	while ( !in.eof() && in.good())
	{
		in>>s;
		
		vi=static_cast<BYTE>(s);

		if (in.good())				
			_l.push_back(vi);
	}
};

template<typename T>
void TSTL2String<T>::ToSTL(LPCTSTR str, std::vector<T>& v)
{
	std::_tistringstream in(static_cast<const TCHAR*>(str));
	T vi;

	v.clear();
	// reading data...
	while ( !in.eof()  && in.good())
	{
		in>>vi;

		if (in.good())
			v.push_back(vi);
	}
};

template<typename T>
void TSTL2String<T>::ToSTL(LPCTSTR str, std::valarray<T>& v)
{
	std::_tistringstream in(static_cast<const TCHAR*>(str));
	std::vector<T> lVi;
	T vi;

	// reading data...
	while ( !in.eof() && in.good())
	{
		in>>vi;
		if (in.good())
			lVi.push_back(vi);
	}

	// ouputting to vector
	v.resize(lVi.size());
	for (UINT i =0;i<v.size();i++)
	{
		v[i]=lVi[i];
	}
};

template<typename T>
void TSTL2String<T>::ToSTL(LPCTSTR str, std::list<T>& _l)
{
	std::_tistringstream in(static_cast<const TCHAR*>(str));
	T vi;

	_l.clear();
	// reading data...
	while ( !in.eof()  && in.good())
	{
		in>>vi;

		if (in.good())
			_l.push_back(vi);
	}
};

template<typename T>
void TSTL2String<T>::ToString(const std::valarray<T>& v, CString& str)
{
	std::_tostringstream out;

	for (UINT i = 0;i<v.size();i++)
	{
		out<<v[i]<<_T(" ");
		if ((i+1)%100==0)
			out<<std::endl;
	}

	str=out.str().c_str();
} ;

template<typename T> 
void TSTL2String<T>::ToString(const std::vector<T>& v, CString& str)
{
	std::_tostringstream out;

	for (UINT i = 0;i<v.size();i++)
	{
		out<<v[i]<<_T(" ");
		if ((i+1)%100==0)
			out<<std::endl;
	}

	str=out.str().c_str();
};

template<typename T>
void TSTL2String<T>::ToString(const std::list<T>& _l, CString& str)
{
	std::_tostringstream out;
	int j;
	std::list<T>::const_iterator i;
	for (i = _l.begin(),j=0;i!=_l.end();i++,j++)
	{
		out<<*i<<_T(" ");
		if ((j+1)%100==0)
			out<<std::endl;
	}

	str=out.str().c_str();
};

};

#endif
